package com.houger.ddd.seckill.template.service;

import com.houger.ddd.common.utils.BusinessException;
import com.houger.ddd.common.utils.StatusCode;
import com.houger.ddd.seckill.application.MapperUtils;
import com.houger.ddd.seckill.infra.entity.ActivityRewardItem;
import com.houger.ddd.seckill.infra.entity.ActivityTemplateBasic;
import com.houger.ddd.seckill.infra.entity.ActivityTemplateBizConfig;
import com.houger.ddd.seckill.infra.entity.ActivityTemplateUiConfig;
import com.houger.ddd.seckill.repository.ActivityRewardItemRepository;
import com.houger.ddd.seckill.repository.ActivityTemplateBasicRepository;
import com.houger.ddd.seckill.repository.ActivityTemplateBizConfigRepository;
import com.houger.ddd.seckill.repository.ActivityTemplateUiConfigRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Houger.
 * @date 2024年05月16日 18:08.
 */
@Service
public class ActivityTemplateService {
    @Resource
    private ActivityTemplateBasicRepository activityTemplateBasicRepository;
    @Resource
    private ActivityTemplateBizConfigRepository activityTemplateBizConfigRepository;
    @Resource
    private ActivityTemplateUiConfigRepository activityTemplateUiConfigRepository;
    @Resource
    private ActivityRewardItemRepository activityRewardItemRepository;

    public Long saveTemplateBasic(final ActivityTemplateBasic basic, final String userName) {
        if (Objects.isNull(basic.getId())){
            basic.setCreateBy(userName);
            return activityTemplateBasicRepository.saveAndFlush(basic).getId();
        }
        final ActivityTemplateBasic activityTemplateBasic = activityTemplateBasicRepository.findById(basic.getId())
            .map(o -> {
                MapperUtils.copy(basic, o);
                o.setUpdateBy(userName);
                return o;
            }).orElse(basic);


        return activityTemplateBasicRepository.saveAndFlush(activityTemplateBasic).getId();
    }

    public void saveTemplateBizConfig(final ActivityTemplateBizConfig bizConfig,String userName) {
        if (Objects.isNull(bizConfig.getTemplateId())){
            bizConfig.setCreateBy(userName);
            activityTemplateBizConfigRepository.save(bizConfig);
            return;
        }
        final ActivityTemplateBizConfig activityTemplateBizConfig = activityTemplateBizConfigRepository.findByTemplateId(bizConfig.getTemplateId())
            .map(o -> {
                MapperUtils.copy(bizConfig, o);
                o.setCreateBy(userName);
                return o;
            }).orElse(bizConfig);
        activityTemplateBizConfigRepository.save(activityTemplateBizConfig);
    }

    public void saveTemplateUiConfig(final ActivityTemplateUiConfig uiConfig,String userName) {
        if (Objects.isNull(uiConfig.getTemplateId())){
            activityTemplateUiConfigRepository.save(uiConfig);
            return;

        }
        final ActivityTemplateUiConfig activityTemplateUiConfig = activityTemplateUiConfigRepository.findByTemplateId(uiConfig.getTemplateId())
            .map(o -> {
                MapperUtils.copy(uiConfig, o);
                o.setCreateBy(userName);
                return o;
            }).orElse(uiConfig);
        activityTemplateUiConfigRepository.save(activityTemplateUiConfig);
    }

    public void saveTemplateRewardItemList(final List<ActivityRewardItem> rewardItemList,String userName) {
        activityRewardItemRepository.deleteByTemplateId(rewardItemList.get(0).getTemplateId());
        rewardItemList.forEach(r-> r.setCreateBy(userName));
        activityRewardItemRepository.saveAll(rewardItemList);
    }


    public ActivityTemplateBasic queryBasicById(final Long templateId) {
        return activityTemplateBasicRepository.findById(templateId).orElseThrow(()->new BusinessException(StatusCode.NOT_FOUND_IN_DB));
    }

    public ActivityTemplateBasic queryBasicByCode(final String templateCode) {
        return activityTemplateBasicRepository.findByTemplateCode(templateCode);
    }

    public int updateStatus(final  Long templateId,final int from, final int to,final Long version) {
        if (Objects.isNull(version)){
            return activityTemplateBasicRepository.updateTemplateStatus(templateId,from,to);
        }
        return activityTemplateBasicRepository.updateTemplateStatus(templateId,from,to,version);
    }
}
